package com.zhika.zishi.adapter;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhika.zishi.R;
import com.zhika.zishi.beans.AnswererBean;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/21.
 */
public class Answerer2Adapter extends BaseAdapter {

    private ArrayList<AnswererBean> answererList;

    private Context context;

    private Handler handler;

    public Answerer2Adapter(Context context,ArrayList<AnswererBean> answererList,Handler handler){
        this.context = context;

        this.answererList = answererList;

        this.handler = handler;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public int getCount() {
        return answererList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder viewHolder;
        if (convertView == null){
            view = LayoutInflater.from(context).inflate(R.layout.item_2_of_my_gird_view,null);
            viewHolder = new ViewHolder();
            viewHolder.headerImageView = (ImageView) view.findViewById(R.id.item_2_of_my_grid_view_header_image_view);
            viewHolder.deleteImageView = (ImageView) view.findViewById(R.id.item_2_of_my_gird_view_delete_image_view);
            viewHolder.nicknameText = (TextView) view.findViewById(R.id.item_2_of_my_grid_view_nickname_text);

            view.setTag(viewHolder);
        }else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
            resetViewHolder(viewHolder);
        }

        final AnswererBean bean = answererList.get(position);

        viewHolder.headerImageView.setImageResource(R.drawable.ic_launcher);
        viewHolder.nicknameText.setText(bean.getNicknameStr());

        viewHolder.deleteImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                answererList.remove(bean);
                notifyDataSetChanged();

                Message message = Message.obtain();
                Bundle bundle = new Bundle();
                bundle.putSerializable("selected_answerer",bean);
                message.setData(bundle);
                message.what = 2;
                handler.sendMessage(message);
            }
        });

        return view;
    }

    private void resetViewHolder(ViewHolder holder) {
        holder.headerImageView.setImageBitmap(null);

        holder.nicknameText.setText(null);
    }

    private class ViewHolder {
        private ImageView headerImageView;

        private ImageView deleteImageView;

        private TextView nicknameText;
    }
}
